Repository navigation
[1337] 新增 openai-responses 协议并优化提示词缓存 - #5
Merged
Merged
Conversation
- 新增 (liii pi ai api openai-responses) 协议库家族(core/msg/thinking/stream-state/stream-parse/request/stream) - dispatch 按 provider-api 路由 openai-responses(非流式与流式) - pi-user-normalize-api 支持 responses / openai-responses 简写 - 系统提示置于 input[0]、工具声明稳定有序、store=false,提升 prompt cache 命中率 - off 档思考降为 effort=low(部分模型拒绝 minimal) - 新增 stub 服务器与 6 个测试文件,补充 responses 路由与归一化回归
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
任务背景
任务编号 1337。GF Pi 当前支持 anthropic-messages、openai-completions、google-generative-ai 三套协议,缺少 OpenAI 自 GPT-5 起主推的 Responses API。官方文档明确 GPT-6 Astra / 6.1 Sol 等模型在 Chat Completions 上调用工具会被拒绝,第三方中转也普遍要求
wire_api="responses"。实现内容
新增第四套协议
openai-responses,结构与openai-completions对齐:ai/liii/pi/ai/api/openai-responses.scm:聚合导出层.../openai-responses/core.scm:status/usage 映射、output[]解析、可重试判定.../openai-responses/msg.scm:input 扁平 item、扁平工具声明、系统提示置于input[0]、prompt cache key.../openai-responses/thinking.scm:reasoning.effort+reasoning.summary.../openai-responses/stream-state.scm、stream-parse.scm:流式状态与 SSE 事件解析.../openai-responses/request.scm、stream.scm:非流式与流式请求ai/liii/pi/ai/dispatch.scm:非流式与流式各增加openai-responses路由分支code/liii/pi/code/config/user-providers.scm:responses/openai-responses简写归一化流式解析派发的内部事件(
text_start/text_delta/thinking_*/toolcall_*)与openai-completions完全一致,复用同一套重试、API key 轮换、额度耗尽判定、错误日志落盘、空闲超时与用户中止逻辑。缓存优化(重点)
查证并验证了 OpenAI 的缓存规则:GPT-5/6 仅按序列化后的
input+tools计算缓存前缀,顶层instructions完全不计入缓存(GPT-6.2+ 还会把它排在input之后,落在命中边界外)。若把系统提示写进instructions,缓存命中率会趋近 0。因此本实现:
role="system"消息置于input[0],与会话历史、工具共同构成稳定前缀store=false:无状态请求,每次携带完整input,不依赖服务端会话prompt_cache_key仅对官方api.openai.com端点注入并截断到 64 字符,第三方端点保守不注入实测(gpt-6.2-sol,4194 token 前缀,连续三轮):
cacheRead为 0 → 3968 → 3968(约 94% 命中)。对照实测 974 token 前缀三轮均不命中,确认 1024 token 最小可缓存长度门槛生效。修复的缺陷
实测
gpt-6.2-sol以unsupported_value拒绝reasoning.effort="minimal"(仅接受 low/medium/high/xhigh/max),因此 off 档映射改为effort=low。验证
gf fmt --check:通过gf check:通过gf test ai/tests/liii/pi/ai/api/openai-responses:6/6gf test ai/tests/liii/pi/ai/dispatch:4/4gf test code/tests/liii/pi/code/config/user-providers:21/21gf test ai/tests/liii/pi/ai/api/openai-completions(回归):45/45gf test --all:本分支 34 失败,基线本身 40 失败,无回归手动验证(真实中转)
api.sharesai.xyz/gpt-6.2-sol:文本返回 PONG;流式返回 1 2 3;工具调用返回 hello-from-responses;多轮工具链返回 step-one / step-two。已知限制
encrypted_content)未回传,未请求include=["reasoning.encrypted_content"]。实测多轮工具链功能正常,后续如有需要再评估。devel/1337.stem。